package core;

import java.util.Enumeration;
import java.util.List;

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RList;
import org.rosuda.JRI.RVector;
import org.rosuda.JRI.Rengine;

public class RPEngine {
	private static RPEngine instance = null;
	Rengine re;
protected RPEngine(){
	re=new Rengine(null, false, new TextConsole());
    System.out.println("Rengine created, waiting for R");
    System.out.println(System.getProperty("os.name"));
    if (System.getProperty("os.name").equals("Windows 7"))
    {
    	System.out.println("It is Win7!");
    	System.out.println(re.eval("getwd()"));
    	System.out.println(re.eval(".libPaths('C:/Users/Yiannos/Documents/R/win-library/3.1') "));
    	System.out.println(re.eval("library(forecast)"));
    }
    else
    {
    System.out.println(re.eval("getwd()"));
	System.out.println(re.eval(".libPaths('C:/Users/Yiannos/Documents/R/win-library/3.1') "));
	System.out.println(re.eval("library(forecast)"));
    }
	// the engine creates R is a new thread, so we should wait until it's ready
    if (!re.waitForR()) {
        System.out.println("Cannot load R");
        return;
    }
	// Exists only to defeat instantiation.
}

public static RPEngine getInstance() {
    if(instance == null) {
       instance = new RPEngine();
    }
    return instance;
 }


	public void predict(List data)
	{
//		Rengine re=new Rengine(null, false, new TextConsole());
//        System.out.println("Rengine created, waiting for R");
		// the engine creates R is a new thread, so we should wait until it's ready
 //       if (!re.waitForR()) {
  //          System.out.println("Cannot load R");
   //         return;
   //     }
        
    	/* High-level API - do not use RNI methods unless there is no other way
		to accomplish what you want */
	try {
		//REXP x;
		re.eval("mydata <- c(0.5674682, 0.6773514, 0.7316666, 0.8291016, 0.9166814, 1.0106846, 1.1956498) ");
	//	re.eval("setwd(\"C:/Users/Yiannos/Documents\") ");
//		System.out.println(re.eval("getwd()"));
//		System.out.println(re.eval(".libPaths('C:/Users/Yiannos/Documents/R/win-library/3.1') "));
//		System.out.println(re.eval("library(forecast)"));
		System.out.println(re.eval("mydata"));
		System.out.println(re.eval("fit <- ets(mydata)"));
		System.out.println(re.eval("forecast(fit,h=4)"));
		System.out.println("OKK");
		//re.eval("q(\"yes\")");
		return;
		//re.eval("data(iris)",false);
		//System.out.println(x=re.eval("iris"));
		// generic vectors are RVector to accomodate names
		//RVector v = x.asVector();
		//if (v.getNames()!=null) {
		//	System.out.println("has names:");
		//	for (Enumeration e = v.getNames().elements() ; e.hasMoreElements() ;) {
		//		System.out.println(e.nextElement());
		//	}
		//}
		// for compatibility with Rserve we allow casting of vectors to lists
		//RList vl = x.asList();
		//String[] k = vl.keys();
		//if (k!=null) {
		//	System.out.println("and once again from the list:");
		//	int i=0; while (i<k.length) System.out.println(k[i++]);
		//}			

		// get boolean array
		//System.out.println(x=re.eval("iris[[1]]>mean(iris[[1]])"));
		// R knows about TRUE/FALSE/NA, so we cannot use boolean[] this way
		// instead, we use int[] which is more convenient (and what R uses internally anyway)
		//int[] bi = x.asIntArray();
		//{
		 //   int i = 0; while (i<bi.length) { System.out.print(bi[i]==0?"F ":(bi[i]==1?"T ":"NA ")); i++; }
		 //   System.out.println("");
		//}
		
		// push a boolean array
		//boolean by[] = { true, false, false };
		//re.assign("bool", by);
		//System.out.println(x=re.eval("bool"));
		// asBool returns the first element of the array as RBool
		// (mostly useful for boolean arrays of the length 1). is should return true
		//System.out.println("isTRUE? "+x.asBool().isTRUE());

		// now for a real dotted-pair list:
		//System.out.println(x=re.eval("pairlist(a=1,b='foo',c=1:5)"));
		//RList l = x.asList();
		//if (l!=null) {
		//	int i=0;
		//	String [] a = l.keys();
		//	System.out.println("Keys:");
		//	while (i<a.length) System.out.println(a[i++]);
		//	System.out.println("Contents:");
		//	i=0;
		//	while (i<a.length) System.out.println(l.at(i++));
		//}
		//System.out.println(re.eval("sqrt(36)"));
	} catch (Exception e) {
		System.out.println("EX:"+e);
		e.printStackTrace();
	}
        
	}
	
}